import { getStream } from '../Core/connect'
import { Object3D } from './Object3D'
import * as MathUtils from '../Math/MathUtils'
import { Vector2 } from '../Math/Vector2'
import { Vector3 } from '../Math/Vector3'

export class Text extends Object3D {
    readonly type: string
    Offset?: Vector2
    color?: Vector3
    alpha?: Number
    constructor(name: string, content: string,Offset:Vector2 = new Vector2(0, 0),color: Vector3 = new Vector3(1,1,1), alpha:Number = 1.0) {
        super({name: name, type: 'text'})
        this.type = 'text'
        this.Offset = Offset
        this.color = color
        this.alpha = alpha
        this.setText(content)
    }
    setText(content: string) {
        const stream = getStream()
        const str = {textFunction: 'setText', ids: `${this.uuid}`, content:`${content}`, color: this.color, alpha: this.alpha}
        stream.emitUIInteraction(str)
    }
    setLongLat(position:Vector3){
        this.position = position
        const stream = getStream()
        const str = {textFunction: 'setLonLat', ids: `${this.uuid}`,position:`${this.position.x},${this.position.y},${this.position.z}`}
        stream.emitUIInteraction(str)
    }
    setTextSize(size: Vector2) {
        const stream = getStream()
        const str = {textFunction: 'setTextSize', ids: `${this.uuid}`, size:`${size.x},${size.y}`}
        stream.emitUIInteraction(str)
    }
    setTextColor(color: Vector3, alpha:Number) {
        this.color = color
        this.alpha = alpha
        const stream = getStream()
        const str = {textFunction: 'setTextColor', ids: `${this.uuid}`, color: color, alpha: alpha}
        stream.emitUIInteraction(str)
    }
    //设置偏移
    setTextOffset(Offset: Vector2) {
        this.Offset = Offset
        const stream = getStream()
        const str = {textFunction: 'setTextOffset', ids: `${this.uuid}`, Offset: `${this.Offset.x},${this.Offset.y}`}
        stream.emitUIInteraction(str)
    }
    setClick(fun: Function) {
        const stream = getStream()
        const sessionId = MathUtils.generateUUID()
        const str = {textFunction: 'setTextClick', ids: `${this.uuid}`, sessionId: sessionId}
        stream.emitUIInteraction(str)
        const getTextClick = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.sessionId === sessionId) {
                fun(this)
            }
        }
        stream.addResponseEventListener(`textClick${this.uuid}`, getTextClick)
    }
    // 开启右键检测
    setRightClick(fun: Function) {
        const stream = getStream()
        const sessionId = MathUtils.generateUUID()
        const str = {textFunction: 'setRightClick', ids: `${this.uuid}`, sessionId: sessionId}
        stream.emitUIInteraction(str)
        const getTextRightClick = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.sessionId === sessionId) {
                fun(this)
            }
        }
        stream.addResponseEventListener(`setRightClick${this.uuid}`, getTextRightClick)
    }
}